/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework1server;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author apple
 */
public class ServerThread implements Runnable {

    private Socket client;
    private int chances;
    private int guess; 
    private int score;
    private char[] cArray;
    private boolean gameover = true;
    List<String> wordArray = new ArrayList<String>();
    List<Character> letters = new ArrayList<Character>();
    String word;
    BufferedReader br;
    
    public ServerThread(Socket client) {
        this.client=client;
    }

    @Override
    public void run() {
        try {
        	score = 0;
            FileInputStream wordsFile = new FileInputStream("dictionary/word.txt");
            br = new BufferedReader(new InputStreamReader(wordsFile));
            
            String new_word = br.readLine();
            while(new_word != null){
            	wordArray.add(new_word);
            	new_word = br.readLine();
            }
            
            wordsFile.close();
            
        } catch (FileNotFoundException e) {
            System.out.println(e.toString());
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}       
        
        BufferedReader in;
            try {
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                
                while(true){
                    String command = in.readLine();
                    System.out.println(command + "\n");

                    if(command.equals("new_game")){
                    	letters.clear();
                    	new_game();
                    }
                    else if(!gameover){
                    		guess(command,client);                    	
                    	
                    }
                    
                    else {
                    	sendMsg(client, "Please start a new game.");
                    }
                   
                }
            } catch (IOException e) {
                System.out.println(e.toString());
            }
    }
    
    public void guess(String c, Socket client) {
        chances--;
        System.out.print("Chances left: " + chances + "\n");
        char client_input = c.toCharArray()[0];
        String result = letters.toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(",", " ");

        if (chances > 0) {

            if (c.equals(word)) {
                System.out.println("win");
                score++;
                gameover = true;
                sendMsg(client, "You win! Congratulations!!! \t" + "The word is: " + word + "\t Score: " + score);
                letters.clear();

            } else if ((c.length() == 1) && (word.contains(c)) && (!letters.contains(client_input))) {

                for (int i = 0; i < cArray.length; i++) {

                    if (cArray[i] == client_input) {

                        letters.set(i, client_input);
                        guess++;

                    }

                }

                if (guess == cArray.length) {
                    System.out.println("win");
                    score++;
                    gameover = true;
                    sendMsg(client, "You win! Congratulations!!! \t" + "The word is: " + word + "\t Score: " + score);
                    letters.clear();
                    guess = 0;

                } else {
                    result = letters.toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(",", " ");
                    System.out.println("correct");
                    sendMsg(client, result + " \t Bingo!" + "\t You have " + chances + " chances left.");
                }

            } else {
                result = letters.toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(",", " ");
                sendMsg(client, result + "\t You have " + chances + " chances left. Please try again.");
            }

        } else if (chances == 0) {
            if (c.equals(word)) {
                System.out.println("win");
                score++;
                gameover = true;
                sendMsg(client, "You win! Congratulations!!! \t" + "The word is: " + word + "\t Score: " + score);
                letters.clear();
            } else if ((c.length() == 1) && (word.contains(c))) {

                for (int i = 0; i < cArray.length; i++) {

                    if (cArray[i] == client_input) {

                        letters.set(i, client_input);
                        guess++;

                    }

                }

                if (guess == cArray.length) {
                    System.out.println("win");
                    gameover = true;
                    score++;
                    sendMsg(client, "You win! Congratulations!!!  " + "The word is: " + word + ".    Score: " + score);
                    letters.clear();
                    guess = 0;

                } else {
                    gameover = true;
                    score--;
                    sendMsg(client, "Game Over. The word is: " + word + ".    Score: " + score);
                    letters.clear();
                    guess = 0;

                }

            } else {
                gameover = true;
                score--;
                sendMsg(client, "Game Over. The word is: " + word + ".    Score: " + score);
                letters.clear();
                guess = 0;
            }

        }

    }


    public void sendMsg(Socket client, String msg){
    	PrintWriter out;

       try {        	
            out = new PrintWriter(client.getOutputStream(),true);
            out.println(msg);
            System.out.print(msg+"\n");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    private void new_game(){
    	gameover = false;
        Random r= new Random();
        word = wordArray.get(r.nextInt(wordArray.size()));

        chances = word.length();;
        guess = 0; 
        
        cArray = word.toCharArray(); 
        
    	for(int i=0; i<cArray.length;i++){
    		letters.add(i, '_'); 
    	}
        
        System.out.print(word+"\n");
        
        sendMsg(client,letters.toString().replaceAll("\\[", "").replaceAll( "\\]", "").replaceAll(",", " ") + "    You have " + chances + " chances to guess the word.");
	
    }
    
}
